#!/usr/bin/perl
use strict;
use warnings;
use CGI ();

#use CGI::Carp qw(fatalsToBrowser);
use MIME::Lite ();
use Contact;
use Storable qw(lock_store);
my $sender_email          = "\"Woei Shyang\" <woei\@flinders.ocfaustralia.org>";
my $error_email           = "\"Woei Shyang\" <woei\@flinders.ocfaustralia.org>";
my $debug_email           = "\"Woei Shyang\" <woei\@flinders.ocfaustralia.org>";
my @contact_fields        = ( 'name', 'family_name', 'email', 'mobile', 'country' );
my @unique_contact_fields = ( 'email', 'mobile' );
my $cgi                   = new CGI;
my $rv;
my $contactData = {};


while (my $line = <>) {
    chomp($line);
    
    my $c = Contact->new('email' => $line);
    my $rv;
    $rv = $c->load( 'speculative' => 1 );
    if (!$rv) {
        # existing value doesn't exist
        my $uname = $line;
        $uname =~ s/\@.+//;
        $c->name($uname);
        $c->family_name('');
        $c->email($line);
        $c->mobile('');
        $c->country('');
    }
    sendWelcomeEmail({'name' => $c->email, 'email' => $c->email});
    $c->save();
    print $line . " processed\n";
}

sub sendWelcomeEmail {
  my $params = shift              || {};
  my $name   = $params->{'name'}  || '';
  my $email  = $params->{'email'} || '';
  my $bodytext = "Hi $name,

Hope you can still remember the chapter of your life in which your life has been touched and transformed transformed through the ministry of the Overseas Christian Fellowship in your student days...

As the saying goes, \"Once an OCFer, always an OCFer\" :)

You may have since moved on from the ministry of OCF to serve in the area of church ministry since then. Or perhaps you have just transitioned from student life and is about to embark upon the next chapter of your life. Or even perhaps you have been especially blessed and even ended up marrying another ex-OCFer, and is now raising up the next generation of OCFers :)

Regardless of where you are in life now, it's necessary that we keep our vision clear and our hope aflame, which is the theme of our upcoming OCF 50th Convention - \"Restoring Vision, Renewing Hope\" that is to be held in Melacca at the end of this year.

Your email $email was collated by our OCF Convention committee as being one that belongs to an ex-OCFer. We only have partial or incomplete contact details for you at this time (which is why the greeting at the beginning of the email would have looked really odd), and so if you'd like to stay informed and hook up with other fellow 'oldies' and 'dinosaurs' from your time, please visit our convention website at http://www.ocfconvention.org and update your particulars there.

If you encounter any errors or glitches while navigating through our Convention website, don't hesitate in letting me know as well.

Kind regards,
Woei Shyang
(OCF Convention Website Assistant)
";
  my $msg = new MIME::Lite( From => $sender_email, To => "\"$name\" <$email>", Subject => "Reaching out, building up, and sending back", Type => "TEXT", Data => $bodytext, );
  $msg->send();
} ## end sub sendConfirmationEmail
